package com.example.template.controller;
import com.example.template.common.annontation.RequiredPermission;
import com.example.template.common.api.ApiResult;
import com.example.template.entity.Permission;
import com.example.template.service.PermissionService;
import com.example.template.utils.JwtUtil;
import com.example.template.utils.RedisUtil;
import com.example.template.vo.MenuCardVO;
import com.example.template.vo.MenuVO;
import org.apache.commons.lang3.StringUtils;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @className: MenuController
 * @description: 菜单控制器
 * @author: Kuangkeli
 * @date: 2024/01/29 10:10
 **/

@RestController
@RequestMapping("/api/menu")
public class MenuController {
    private Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private RedisUtil redisUtil;


    @RequiredPermission("system:menu:list")
    @RequestMapping("/getMenuList")
    public ApiResult getMenuList(HttpServletRequest request){
        Set<Permission> menuList= null;
        String userIdStr = String.valueOf(request.getAttribute("userId"));
        if(StringUtils.isNotBlank(userIdStr)){
            long userId = Long.parseLong(userIdStr);
            menuList = permissionService.getMenuListByUserId(userId);
        }
        ArrayList<MenuVO> menuVOList = new ArrayList<>();
        for (Permission p: menuList) {
            MenuVO e = new MenuVO();
            BeanUtils.copyProperties(p,e);
            menuVOList.add(e);
        }
        return ApiResult.success(menuVOList);
    }

    @RequiredPermission("system:menu:list")
    @RequestMapping("/getMenuTree")
    public ApiResult getMenuTree(HttpServletRequest request){
        //先取出用户id 根据用户id 查找出关联的角色  查出角色列表所拥有的最高级父级列表  在根据父级列表id
        List<MenuCardVO> menuCardVOList = null;
        String userIdStr  = String.valueOf(request.getAttribute("userId"));
        if (StringUtils.isNotBlank(userIdStr)){
            Long userId = Long.parseLong(userIdStr);
            menuCardVOList = permissionService.getMenuTreeByUserId(userId);
            System.out.println(menuCardVOList);
        }
        return ApiResult.success(menuCardVOList);
    }

    @RequiredPermission("system:menu:query")
    @GetMapping("/get")
    public ApiResult get(@RequestParam("Id") Long Id) {
        return ApiResult.success(permissionService.selectByPrimaryKey(Id));
    }

    @RequestMapping("/getPermTree")
    public ApiResult getPermTree(HttpServletRequest request){
        List<Permission> menus = permissionService.selectAll();
        return ApiResult.success( permissionService.buildMenuTreeSelect(menus));
    }


}
